home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / codeview.arc / COUNT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-06-27  |  3.6 KB  |  131 lines

  1. #include <stdio.h>
  2. #include <io.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6. #define        BUFFSIZE        512     /* Small buffer for debugging   */
  7. #define TRUE   1
  8. #define FALSE  0
  9. #define        FACTOR  1.1     /* Vowels per syllable in typical file  */
  10.  
  11. /* Conditional operator prevents divide by zero        */
  12.  
  13. #define WPS    (float)words / (sentences ? sentences : 1)
  14. #define LPW    (float)letters / (words ? words : 1)
  15. #define SPW    (vowels * FACTOR) / (words ? words : 1)
  16.  
  17. int    bytes = 0, characters = 0, words = 0, lines = 0;
  18. int    letters = 0, vowels = 0, sentences = 0;
  19.  
  20. char   buffer[BUFFSIZE];
  21.  
  22. main(argc,argv)
  23. int    argc;
  24. char   *argv[];
  25. {
  26.        FILE    *stream;
  27.        int     namebuf[15];
  28.        char    *name;
  29.        int     numread;
  30.        char    inword = FALSE;
  31.  
  32.        /* Get a file if one was not specified as an argument   */
  33.  
  34.        if (argc > 1)
  35.                name = argv[1];
  36.        else {
  37.                printf("Enter file name: ");
  38.                name = gets(namebuf);
  39.        }
  40.  
  41.        /* Open file in binary mode     */
  42.  
  43.        if ((stream = fopen(name,"rb")) == NULL)
  44.                return (1);
  45.  
  46.        /* Read file buffers, passing bytes read and inword status      */
  47.  
  48.        while ((numread = fread(buffer,1,BUFFSIZE,stream)) != 0)
  49.                inword = countwords(inword,numread);
  50.  
  51.        /* Calculate and print the results      */
  52.  
  53.        printf("\n\n\t\tFile statistics\n\n");
  54.        printf("\t\tBytes: %d \n",bytes);
  55.        printf("\t\tCharacters: %d \n",characters);
  56.        printf("\t\tLetters: %d \n",letters);
  57.        printf("\t\tVowels: %d \n",vowels);
  58.        printf("\t\tConsonants %d \n",letters - vowels);
  59.        printf("\t\tWords: %d \n",words);
  60.        printf("\t\tLines: %d \n",lines ? lines : 1);
  61.        printf("\t\tSentences: %d \n",sentences);
  62.        printf("\t\tWords per sentence: %-.1f \n",WPS);
  63.        printf("\t\tLetters per word: %-.1f \n",LPW);
  64.        printf("\t\tEstimated syllables per word: %-.1f \n",SPW);
  65.        return (0);
  66. }
  67.  
  68. /*
  69.  *  Analyze the chars in one buffer.
  70.  *
  71.  *  Increment bytes.  For each char, increment characters,
  72.  *  lines, and/or words if appropriate.  For each character,
  73.  *  call analyze function.  (A character is defined as a
  74.  *  printable ASCII code.)
  75.  */
  76.  
  77. countwords(inword,numread)
  78. char   inword;
  79. int    numread;
  80. {
  81.        int     count;
  82.        char    code;
  83.  
  84.        bytes += numread;
  85.        for (count = 0; count <= numread; ++count) {
  86.                code = buffer[count];
  87.                if (code == '\n')
  88.                        ++lines;
  89.                if (!inword) {
  90.                        if (code > ' ') {
  91.                                analyze(code,inword);
  92.                                inword = TRUE;
  93.                                ++words;
  94.                                ++characters;
  95.                        }
  96.                }
  97.                else {
  98.                        if (code <= ' ')
  99.                                inword = FALSE;
  100.                        else {
  101.                                ++characters;
  102.                                analyze(code,inword);
  103.                        }
  104.                }
  105.        }
  106.        return(inword);
  107. }
  108.  
  109. /*
  110.  *  Analyze a character.
  111.  *
  112.  *  Increment letters, vowels, and/or sentences
  113.  *  if appropriate.
  114.  */
  115.  
  116. analyze(code,inword)
  117. char   code;
  118. char   inword;
  119. {
  120.        if (isalpha(code)) {
  121.                ++letters;
  122.                if ((strchr("AEIOUaeiou",code)) || (strchr("Yy",code) && inword))
  123.                        ++vowels;
  124.        }
  125.        else {
  126.                if (strchr(".!?",code))
  127.                        ++sentences;
  128.        }
  129. }
  130.  
  131.